//
//  ReplaceHOmeManager.m
//  随拍
//
//  Created by lanou3g on 16/1/22.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "ReplaceHOmeManager.h"

#define locationUrl @"http://q.chanyouji.com/api/v1/search/featured.json"

#import "ReplaceHomeModel.h"



@implementation ReplaceHOmeManager

+(ReplaceHOmeManager *)sharedReplaceHomeManager{
    static ReplaceHOmeManager *replaceManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        replaceManager = [[ReplaceHOmeManager alloc] init];
        [replaceManager requstNetWork];
        
    });
    
    return replaceManager;
}


-(void)requstNetWork{
    
    
    NSURL *url = [NSURL URLWithString:locationUrl];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
      
        
        for (NSDictionary *dict in dic[@"data"][@"destinations"]) {
            ReplaceHomeModel *replaceModel = [[ReplaceHomeModel alloc] init];
            
            [replaceModel setValuesForKeysWithDictionary:dict];
            
            [self.onearray addObject:replaceModel];
        }
     
        dispatch_async(dispatch_get_main_queue(), ^{
           
            self.updateUI();
            
            
        });
        
        
        
    }];
    
    [task resume];
    
}


-(ReplaceHomeModel *)returnModelIndex:(NSInteger)index{
    return [self.onearray objectAtIndex:index];
}

-(NSMutableArray *)onearray{
    if (!_onearray) {
        _onearray = [NSMutableArray array];
    }
    return _onearray;
}





@end
